package com.jsmtr.rms.controller;

import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.jsmtr.rms.entity.BaseMessageConfig;
import com.jsmtr.rms.utils.StringTools;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.jsmtr.rms.entity.BaseConfig;
import com.jsmtr.rms.entity.BaseEmailConfig;
import com.jsmtr.rms.entity.BaseRegtips;
import com.jsmtr.rms.entity.vo.BaseConfigForm;
import com.jsmtr.rms.service.IConfigService;
import com.jsmtr.rms.service.IUtilService;

@Controller
@RequestMapping("/sysconfig")
public class SysConfigController {

    @Resource
    private IUtilService utilService;

    @Resource
    private IConfigService configService;

    //首页
    @RequestMapping("/index")
    public String Index(HttpServletRequest request, Model model) {
        try {
            int index = 0;
            List<BaseConfig> sysConfigs = configService.getConfigs("sys");
            StringBuilder sys = new StringBuilder();
            for (BaseConfig sysConfig : sysConfigs) {
                sys.append("<tr><td>" + sysConfig.getCode() + "<input type='hidden' name='config[" + index + "].code' value='" + sysConfig.getCode() + "' /></td>");
                sys.append("<td><input type='hidden' name='config[" + index + "].name' value='" + sysConfig.getName() + "' />" + sysConfig.getName() + "</td>");
                sys.append("<td><div><input type='text' name='config[" + index + "].cont' value='" + sysConfig.getCont() + "' datatype='*' nullmsg='内容不能为空' /><input type='hidden' name='config[" + index + "].groupid' value='sys' /></div></td></tr>");
                index++;
            }
            model.addAttribute("sys", sys);

            List<BaseConfig> dxmbConfigs = configService.getConfigs("dxmb");
            StringBuilder dxmb = new StringBuilder();
            for (BaseConfig config : dxmbConfigs) {
                dxmb.append("<tr><td><div><input type='text' name='config[" + index + "].code' value='" + config.getCode() + "'  datatype='*' nullmsg='编码不能为空'/></div></td>");
                dxmb.append("<td><div><input type='text' name='config[" + index + "].name' value='" + config.getName() + "'  datatype='*' nullmsg='名称不能为空'/></div></td>");
                dxmb.append("<td><div><input type='text' name='config[" + index + "].cont' value='" + config.getCont() + "'  datatype='*' nullmsg='内容不能为空'/><input type='hidden' name='config[" + index + "].groupid' value='dxmb' /></div></td>");
                dxmb.append("<td><button type='button' class='del' onfocus='blur()'></button></td></tr>");
                index++;
            }
            model.addAttribute("dxmb", dxmb);

            List<BaseConfig> dzmbConfigs = configService.getConfigs("dzmb");
            StringBuilder dzmb = new StringBuilder();
            for (BaseConfig config : dzmbConfigs) {
                dzmb.append("<tr><td><div><input type='text' name='config[" + index + "].code' value='" + config.getCode() + "' datatype='*' nullmsg='编码不能为空'/></div></td>");
                dzmb.append("<td><div><input type='text' name='config[" + index + "].name' value='" + config.getName() + "' datatype='*' nullmsg='名称不能为空' /></div></td>");
                dzmb.append("<td><div><input type='text' name='config[" + index + "].cont' value='" + config.getCont() + "' datatype='*' nullmsg='内容不能为空' /><input type='hidden' name='config[" + index + "].groupid' value='dzmb' /></div></td>");
                dzmb.append("<td><button type='button' class='del' onfocus='blur()'></button></td></tr>");
                index++;
            }
            model.addAttribute("dzmb", dzmb);



            BaseEmailConfig email = utilService.getEmailConfig();
            model.addAttribute("email", email);

            BaseMessageConfig messageConfig=utilService.getMessageConfig();
            model.addAttribute("message",messageConfig);

            String szjlcs=utilService.getConfig("szjlcs");
            model.addAttribute("szjlcs",szjlcs);

            String xzjlcs=utilService.getConfig("xzjlcs");
            model.addAttribute("xzjlcs",xzjlcs);

            String autocloseproject=utilService.getConfig("autocloseproject");
            model.addAttribute("autocloseproject",autocloseproject);

            String mszysx=utilService.getConfig("mszysx");
            model.addAttribute("mszysx",mszysx);

            String bszysx=utilService.getConfig("bszysx");
            model.addAttribute("bszysx",bszysx);
            String tjzysx=utilService.getConfig("tjzysx");
            model.addAttribute("tjzysx",tjzysx);

            String beAnswer=utilService.getConfig("beAnswer");
            model.addAttribute("beAnswer",beAnswer);

            BaseConfig imgpathBaseconfig = utilService.findConfig("imgpath");
            BaseConfig mappathBaseconfig = utilService.findConfig("mappath");
            String imgpath = imgpathBaseconfig != null? imgpathBaseconfig.getCont(): "";
            String mappath = mappathBaseconfig != null? mappathBaseconfig.getCont(): "";
            model.addAttribute("imgpath", imgpath);
            model.addAttribute("mappath", mappath);

            BaseRegtips tips = utilService.getRegtips();
            model.addAttribute("tips", tips);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return "/SysAdmin/sysconfig/index";
    }

    //保存系统配置
    @RequestMapping(value = "/SaveAction", method = RequestMethod.POST)
    public void SaveAction(HttpServletRequest request, HttpServletResponse response, BaseConfigForm config, BaseEmailConfig email, BaseRegtips tips,BaseMessageConfig message) {
        try {
            List<BaseConfig> configs = config.getConfig();
            List<Object> editlist = new ArrayList<Object>();
            List<BaseConfig> addlist = new ArrayList<BaseConfig>();
            List<BaseConfig> dellist = new ArrayList<BaseConfig>();
            for (BaseConfig conf : configs) {
                if(conf.getGroupid()!=null){
                    if (conf.getGroupid().equals("sys")) {
                        conf.setGroupname("系统配置");
                        editlist.add(conf);
                    } else if (conf.getGroupid().equals("dxmb")) {
                        conf.setGroupname("短信模版");
                        addlist.add(conf);
                    } else if (conf.getGroupid().equals("dzmb")) {
                        conf.setGroupname("地址模版");
                        addlist.add(conf);
                    }
                }

            }

            dellist.addAll(configService.getConfigs("dxmb"));
            dellist.addAll(configService.getConfigs("dzmb"));

            BaseMessageConfig _message = utilService.getMessageConfig();
            String mhost=request.getParameter("messagehost");
            if(StringTools.trimNotEmpty(mhost)) {
                _message.setHost(mhost);
            }
            String mpassword=request.getParameter("messagepassword");
            if(StringTools.trimNotEmpty(mpassword)) {
                _message.setPassword(mpassword);
            }
           /* _message.setPort(message.getPort());*/
            String musername=request.getParameter("messageusername");
            if(StringTools.trimNotEmpty(musername)) {
                _message.setUsername(musername);
            }
            editlist.add(_message);

           /* BaseEmailConfig _email = utilService.getEmailConfig();
            _email.setHost(email.getHost());
            _email.setPassword(email.getPassword());
            _email.setPort(email.getPort());
            _email.setUsername(email.getUsername());

            editlist.add(_email);
*/
            BaseRegtips _tip = utilService.getRegtips();
            _tip.setRcont(tips.getRcont());
            editlist.add(_tip);

            String szjlcs=request.getParameter("szjlcs");
            if(szjlcs!=null&&szjlcs.length()>0){
                BaseConfig editszjlcs=utilService.findConfig("szjlcs");
                editszjlcs.setCont(szjlcs);
                editlist.add(editszjlcs);
            }
            String xzjlcs=request.getParameter("xzjlcs");
            if(xzjlcs!=null&&xzjlcs.length()>0){
                BaseConfig editxzjlcs=utilService.findConfig("xzjlcs");
                editxzjlcs.setCont(xzjlcs);
                editlist.add(editxzjlcs);
            }

            String mszysx=request.getParameter("mszysx");
            if(mszysx!=null&&mszysx.length()>0){
                BaseConfig editmszysx=utilService.findConfig("mszysx");
                editmszysx.setCont(mszysx);
                editlist.add(editmszysx);
            }

            String bszysx=request.getParameter("bszysx");
            if(bszysx!=null&&bszysx.length()>0){
                BaseConfig editbszysx=utilService.findConfig("bszysx");
                editbszysx.setCont(bszysx);
                editlist.add(editbszysx);
            }

            String tjzysx=request.getParameter("tjzysx");
            if(tjzysx!=null&&tjzysx.length()>0){
                BaseConfig edittjzysx=utilService.findConfig("tjzysx");
                edittjzysx.setCont(tjzysx);
                editlist.add(edittjzysx);
            }

            String autocloseproject=request.getParameter("autocloseproject");
            if(autocloseproject!=null&&autocloseproject.length()>0){
                BaseConfig autoclose=utilService.findConfig("autocloseproject");
                autoclose.setCont(autocloseproject);
                editlist.add(autoclose);
            }

            String imgpath = request.getParameter("imgpath");
            if (imgpath != null && imgpath.length() > 0) {
                BaseConfig imgpathBaseConfig = utilService.findConfig("imgpath");
                if (imgpathBaseConfig != null) {
                    imgpathBaseConfig.setCont(imgpath);
                    editlist.add(imgpathBaseConfig);
                } else {
                    imgpathBaseConfig = new BaseConfig();
                    imgpathBaseConfig.setCode("imgpath");
                    imgpathBaseConfig.setCont(imgpath);
                    imgpathBaseConfig.setName("虚拟目录");
                    imgpathBaseConfig.setGroupid("path");
                    imgpathBaseConfig.setGroupname("目录");
                    utilService.addObject(imgpathBaseConfig);
                }
            }

            String mappath = request.getParameter("mappath");
            if (mappath != null && mappath.length() > 0) {
                BaseConfig mappathBaseConfig = utilService.findConfig("mappath");
                if (mappathBaseConfig != null) {
                    mappathBaseConfig.setCont(mappath);
                    editlist.add(mappathBaseConfig);
                } else {
                    mappathBaseConfig = new BaseConfig();
                    mappathBaseConfig.setCode("mappath");
                    mappathBaseConfig.setCont(mappath);
                    mappathBaseConfig.setName("映射目录");
                    mappathBaseConfig.setGroupid("path");
                    mappathBaseConfig.setGroupname("目录");
                    utilService.addObject(mappathBaseConfig);

                }
            }

            String beAnswer = request.getParameter("beAnswer");
            if (beAnswer != null && beAnswer.length() > 0) {
                BaseConfig beAnswerBaseConfig = utilService.findConfig("beAnswer");
                if (beAnswerBaseConfig != null) {
                    beAnswerBaseConfig.setCont(beAnswer);
                    editlist.add(beAnswerBaseConfig);
                } else {
                    beAnswerBaseConfig = new BaseConfig();
                    beAnswerBaseConfig.setCode("beAnswer");
                    beAnswerBaseConfig.setCont(beAnswer);
                    beAnswerBaseConfig.setName("是否答题");
                    beAnswerBaseConfig.setGroupid("beAnswer");
                    beAnswerBaseConfig.setGroupname("答题设置");
                    utilService.addObject(beAnswerBaseConfig);
                }
            }

            utilService.editList(dellist, editlist, addlist);

            utilService.printJsonResult(true, "操作成功！", "no", response);
        } catch (Exception e) {
            e.printStackTrace();
            utilService.printJsonResult(false, "操作失败！", "no", response);
        }
    }



    @RequestMapping(value = "SaveZysxAction", method = RequestMethod.POST)
    public void SaveZysxAction(HttpServletRequest request, HttpServletResponse response){
        try {
            List<Object> editlist = new ArrayList<Object>();
            String mszysx=request.getParameter("mszysx");
            if(mszysx!=null&&mszysx.length()>0){
                BaseConfig editmszysx=utilService.findConfig("mszysx");
                editmszysx.setCont(mszysx);
                editlist.add(editmszysx);
            }
            utilService.editList(editlist);
            utilService.printJsonResult(true, "操作成功！", "no", response);
        } catch (Exception e) {
            e.printStackTrace();
            utilService.printJsonResult(false, "操作失败！", "no", response);
        }
    }
}
